spb/chat-spboucher-ai
Public
1import { z } from "zod";2import { guarded, json, body } from "@/lib/api";3import { getConversation, getActiveMessages, getAllMessages, updateConversation, deleteConversation } from "@/lib/chat/store";4import { sql } from "@/lib/db";56export const GET = guarded<{ id: string }>(async (req, { params }) => {7 const conv = await getConversation(params.id);8 if (!conv) return json({ error: "Introuvable" }, { status: 404 });9 const all = req.nextUrl.searchParams.get("all") === "1";10 const messages = all ? await getAllMessages(params.id) : await getActiveMessages(params.id);11 // sibling counts for branch navigation12 const sib = await sql<{ parent_id: string | null; role: string; n: number }[]>`SELECT parent_id, role, count(*)::int AS n FROM messages WHERE conversation_id = ${params.id} GROUP BY parent_id, role HAVING count(*) > 1`;13 const attachments = await sql`SELECT id, filename, mime, size_bytes, kind, page_count, message_id, created_at FROM attachments WHERE conversation_id = ${params.id} ORDER BY created_at`;14 const artifacts = await sql`SELECT id, root_id, version, type, language, title, saved, message_id, created_at FROM artifacts WHERE conversation_id = ${params.id} ORDER BY created_at`;15 return json({ conversation: conv, messages, siblings: sib, attachments, artifacts });16});1718export const PATCH = guarded<{ id: string }>(async (req, { params }) => {19 const b = await body(req, z.object({ title: z.string().min(1).max(200).optional(), projectId: z.string().nullable().optional(), modelId: z.string().optional(), pinned: z.boolean().optional(), archived: z.boolean().optional(), settings: z.record(z.string(), z.unknown()).optional() }));20 await updateConversation(params.id, { title: b.title, project_id: b.projectId, model_id: b.modelId, pinned: b.pinned, archived: b.archived, settings: b.settings });21 return json({ conversation: await getConversation(params.id) });22});2324export const DELETE = guarded<{ id: string }>(async (_req, { params }) => { await deleteConversation(params.id); return json({ ok: true }); });25